<?php

class auth
{

	//获取用户userid接口地址
	const GET_USERID_RUL = 'http://api.lolbox.duowan.com/api/v3/player/search';
	public function __construct(){}
	/**
	 * combineUrl 拼接url
	 * @param mixed $baseUrl 
	 * @param mixed $keysArr 
	 * @return mixed 
	 */
	public function combineUrl($baseUrl, $keysArr)
	{
		$combined = $baseUrl . '?';
		$valueArr = array();

		foreach ($keysArr as $key => $val) {
			$valueArr[] = "$key=$val";
		}
		$keyStr = implode('&', $valueArr);
		$combined .= ($keyStr);

		return $combined;
	}


	/**
	 * get_contents get请求
	 * @param mixed $url 
	 * @return mixed 
	 */
	public function get_contents($url)
	{
		if (ini_get("allow_url_fopen") == "1") {
			$response = file_get_contents($url);
		} else {
			$host = array("Host: api.lolbox.duowan.com");
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER, $host);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_URL, $url);
			$response = curl_exec($ch);
			curl_close($ch);
		}

        //-------请求为空
		if (empty($response)) {
			return false;
		}

		$response = $this->jsonp_decode($response);
		if (empty($response)) return false;
		return $response;
	}

	/**
	 * jsonp_decode 解码返回的jsonp数据
	 * @param mixed $jsonp 
	 * @param mixed $assoc 
	 * @return mixed 
	 */
	public function jsonp_decode($jsonp, $assoc = false)
	{
		if ($jsonp[0] !== '[' && $jsonp[0] !== '{') {
			$jsonp = substr($jsonp, strpos($jsonp, '('));
		}
		return json_decode(trim($jsonp, '();'), $assoc);
	}

	/**
	 * get 发送get请求
	 * @param mixed $url 
	 * @param mixed $keysArr 
	 * @return mixed 
	 */
	public function get($url, $keysArr)
	{
		$combined = $this->combineURL($url, $keysArr);
		return $this->get_contents($combined);
	}

	/**
	 * getServerName 获取大区名称
	 * @param mixed $servername 
	 * @return mixed 
	 */
	public function getServerName($servername)
	{
		$server = array(
			'艾欧尼亚' => ' 电信一',
			'祖安' => '电信二',
			'诺克萨斯' => ' 电信三',
			'班德尔城' => '电信四',
			'皮尔特沃夫' => '电信五',
			'战争学院' => '电信六',
			'巨神峰' => '电信七',
			'雷瑟守备' => '电信八',
			'裁决之地' => '电信九',
			'黑色玫瑰' => '电信十',
			'暗影岛' => '电信十一',
			'钢铁烈阳' => '电信十二',
			'均衡教派' => '电信十三',
			'水晶之痕' => '电信十四',
			'影流' => '电信十五',
			'守望之海' => '电信十六',
			'征服之海' => '电信十七',
			'卡拉曼达' => '电信十八',
			'皮城警备' => '电信十九',
			'比尔吉沃特' => '网通一',
			'德玛西亚' => '网通二',
			'弗雷尔卓德' => '网通三',
			'无畏先锋' => '网通四',
			'恕瑞玛' => '网通五',
			'扭曲丛林' => '网通六',
			'巨龙之巢' => '网通七',
			'教育网专区' => '教育一',
			'男爵领域' => '全网一',
		);

		return $server[$servername];
	}
}

?>